package com.example.fastshare.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.example.fastshare.entities.UserEntity;

public class DBUserAdapter extends BaseAdapter {
	private DatabaseAdapter mDatabaseAdapter;
	private Context context;
	private ArrayList<Bitmap> arrayBitMap;

	public DBUserAdapter(Context context) {
		super();
		this.mDatabaseAdapter = new DatabaseAdapter(context);
	}

	public DBUserAdapter(Context context, ArrayList<Bitmap> arrayBitMap) {
		super();
		this.context = context;
		this.arrayBitMap = arrayBitMap;
	}

	public DatabaseAdapter getmDatabaseAdapter() {
		return mDatabaseAdapter;
	}

	public void setmDatabaseAdapter(DatabaseAdapter mDatabaseAdapter) {
		this.mDatabaseAdapter = mDatabaseAdapter;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public ArrayList<Bitmap> getArrayBitMap() {
		return arrayBitMap;
	}

	public void setArrayBitMap(ArrayList<Bitmap> arrayBitMap) {
		this.arrayBitMap = arrayBitMap;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		return null;
	}

	public long insertUser(UserEntity entity) {
		if (entity != null) {
			ContentValues values = new ContentValues();
			values.put(UserEntity.ID, entity.getID());
			values.put(UserEntity.NAME, entity.getName());
			values.put(UserEntity.AVATAR_PATH, entity.getAvatar_Path());

			long result = mDatabaseAdapter.insert(UserEntity.TABLE_NAME, null,
					values);
			Log.i("Info", "Add ok");
			return result;
		} else
			Log.i("Info", "Add no");
			return -1;
	}

	public UserEntity getUserByID(String id) {
		UserEntity result = null;

		String where = UserEntity.ID + "='" + id + "'";
		Cursor cur = mDatabaseAdapter.query(UserEntity.TABLE_NAME, null, where,
				null, null, null, null);

		if (cur != null && cur.getCount() > 0) {
			result = getUserEntity(cur);
		}

		cur.close();
		return result;
	}

	public List<UserEntity> getAllUser() {
		List<UserEntity> result = null;

		String orderASC = UserEntity.ID + " ASC ";

		Cursor cur = mDatabaseAdapter.query(UserEntity.TABLE_NAME, null, null,
				null, null, null, orderASC);

		if (cur != null && cur.getCount() > 0) {
			cur.moveToFirst();
			result = new ArrayList<UserEntity>();
			while (!cur.isAfterLast()) {
				result.add(getUserEntity(cur));
				cur.moveToNext();
			}
		}
		cur.close();
		return result;
	}

	public boolean isEmptyUser() {
		boolean result = true;

		Cursor cur = mDatabaseAdapter.query(UserEntity.TABLE_NAME, null, null,
				null, null, null, null);

		if (cur != null && cur.getCount() > 0) {
			result = false;
		}
		cur.close();
		return result;
	}

	public long deleteUserByID(String id) {
		String where = UserEntity.ID + "='" + id + "'";
		return mDatabaseAdapter.delete(UserEntity.TABLE_NAME, where, null);
	}

	public long deleteAllUsers() {
		return mDatabaseAdapter.delete(UserEntity.TABLE_NAME, null, null);
	}

	private UserEntity getUserEntity(Cursor mCursor) {
		if (mCursor != null) {
			UserEntity values = new UserEntity();
			values.setID(mCursor.getString(mCursor
					.getColumnIndex(UserEntity.ID)));

			values.setName(mCursor.getString(mCursor
					.getColumnIndex(UserEntity.NAME)));
			values.setAvatar_Path(mCursor.getString(mCursor
					.getColumnIndex(UserEntity.AVATAR_PATH)));
			return values;
		}
		return null;
	}
}
